#include "FromMyDialog.h"
#include "ui_FromMyDialog.h"

#include <QDateTime>
#include <QDebug>
#include "Line.h"
#include "Menu.h"
#include "Bed.h"
#include "Operator.h"
#include "MyDialog.h"
FromMyDialog::FromMyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FromMyDialog)
{
    ui->setupUi(this);
    blurEffect = new QGraphicsBlurEffect;
    blurEffect->setBlurRadius(30); // 设置模糊半径，值越大，模糊效果越强
    this->ui->time->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm"));
    connect(&this->timer,&QTimer::timeout,this,[=](){
        this->ui->time->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm"));
    });
    connect(this->siri,&Siri::siriTask,this,&FromMyDialog::siriDoWork);
    this->timer.start(40*1000);
    layoutSiri = new QHBoxLayout;
    //this->installEventFilter(this);

}

FromMyDialog::~FromMyDialog()
{
    delete ui;
}

void FromMyDialog::setFuzzy()
{
        this->setGraphicsEffect(blurEffect); // 将模糊效果应用到窗口上
        //this->update();

}

void FromMyDialog::showSiri()
{
//    layout->addWidget(new Bed);
//    this->bed->ui->downWidget->setLayout(layout);
    layoutSiri->addWidget(FromMyDialog::siri);
    this->ui->siri->setLayout(layoutSiri);
    FromMyDialog::siri->showSiri();
}


void FromMyDialog::siriDoWork(functionFlag flag)
{
    qDebug()<<"FromMyDialog::siri->isActiveWindow():"<<FromMyDialog::siri->isActiveWindow()<<"    this->isActiveWindow():"<<this->isActiveWindow()<<endl;

    if(this->isActiveWindow())
    {
        //if(!FromMyDialog::siri->isActiveWindow())
            showSiri();
        MyDialog*myDia=this->findChild<MyDialog*>();
        if((flag==functionFlag::pump_stop || flag==functionFlag::pump_start) && (myDia->dialogName==MyDialog::LineDialog || myDia->dialogName==MyDialog::BedDialog))
            this->on_exit_clicked();//隐藏当前页面
        if(myDia->dialogName==MyDialog::OperationDialog)
        {
            myDia->siriWork(MyDialog::OperationDialog,flag);
            //
        }
        else if(myDia->dialogName==MyDialog::LineDialog)
        {
            //
            myDia->siriWork(MyDialog::LineDialog,flag);
        }
        else if(myDia->dialogName==MyDialog::BedDialog)
        {
           myDia->siriWork(MyDialog::BedDialog,flag);
        }
        else if(myDia->dialogName==MyDialog::MenuDialog)
        {
            myDia->siriWork(MyDialog::MenuDialog,flag);
        }
        else if(myDia->dialogName==MyDialog::setDialog)
        {
            myDia->siriWork(MyDialog::setDialog,flag);
        }
    }
}

void FromMyDialog::on_exit_clicked()
{
    this->siri->hideSiri();
    this->hide();
}
void FromMyDialog::showSiri_slot()
{qDebug()<<"showSiri_slot"<<endl;
    showSiri();
}

void FromMyDialog::showPatient_slot(QString str)
{
    this->ui->callPush->setText("       "+str);
}

void FromMyDialog::showServerStatus(bool r)
{
    if(r)
        this->ui->serverStatus->setStyleSheet("border-image: url(:/resource/server_blue.png);");
    else
        this->ui->serverStatus->setStyleSheet("border-image: url(:/resource/server_red.png);");
}


//bool FromMyDialog::eventFilter(QObject *watched, QEvent *event)
//{qDebug()<<"事件过滤器"<<endl;
//    if (this->siri->isVisible() && event->type()==QEvent::MouseButtonPress) {
//        // 如果你想拦截这个事件，返回true；否则返回false。
//        this->siri->hide();
//        return true;
//    }
//    return QObject::eventFilter(watched, event);

//}
/* 注意：当有数据来时，并不是一个字节一个字节的进入处理函数，而是只进入一次 */
/* 信号处理函数,当串口有数据可读时，会跳转到该函数执行 */
void io_handler(int sig, siginfo_t *info, void *context)
{
    if(SIGRTMIN != sig)
    return;
    /* 判断串口是否有数据可读 */
    if(POLL_IN == info->si_code) {
        if(read(uart_fd, recive_buf, sizeof(recive_buf))>0) {
            printf("recive_buf = %s \n",recive_buf);
            functionFlag uart_flag=clear_data;
            /* LED灯控制 */
            if(!qstrcmp(recive_buf,QByteArray("ledup\r\n"))) {               //开灯，此时灯的占空比为50%
               uart_flag = functionFlag::light_openhalf;
            } else if(!strcmp(recive_buf,"leddown\r\n")) {      //关灯，此时关闭PWM
                uart_flag = functionFlag::light_close;
            } else if(!strcmp(recive_buf,"led100\r\n")) {      //把灯调亮，此时占空比为100%
                uart_flag = functionFlag::light_openall;
            }

            /* 舵机控制 */
            else if(!strcmp(recive_buf,"tuo1\r\n")) {           //把床调为1档
                uart_flag = functionFlag::bed_1;
            } else if(!strcmp(recive_buf,"tuo2\r\n")) {         //把床调为2档
                uart_flag = functionFlag::bed_2;
            } else if(!strcmp(recive_buf,"tuo3\r\n")) {         //把床调为3档
                uart_flag = functionFlag::bed_3;
            } else if(!strcmp(recive_buf,"tuo4\r\n")) {         //把床调为4档
                uart_flag = functionFlag::bed_4;
            }

            /* 采集控制 */
            else if(!strcmp(recive_buf,"adcstart\r\n")) {        //开始手术
                uart_flag = functionFlag::pump_start;
            } else if(!strcmp(recive_buf,"adcstop\r\n")) {      //停止手术
                uart_flag = functionFlag::pump_stop;
            }

//            /* siri唤醒*/
//            else if(!strcmp(recive_buf,"hello\r\n")) {           //
//                FromMyDialog::siri->siri_flag = true;
//            }
            memset(recive_buf, 0, sizeof(recive_buf));
                emit FromMyDialog::siri->siriTask(uart_flag);
        }
    }
}



void FromMyDialog::on_callPush_clicked()
{
    MyDialog*myDia=this->findChild<MyDialog*>();
    if(myDia->dialogName==MyDialog::OperationDialog)
    {
        myDia->siriWork(MyDialog::OperationDialog,functionFlag::call_cancel);
        //
    }
    else if(myDia->dialogName==MyDialog::LineDialog)
    {
        //
        myDia->siriWork(MyDialog::LineDialog,functionFlag::call_cancel);
    }
    else if(myDia->dialogName==MyDialog::BedDialog)
    {
       myDia->siriWork(MyDialog::BedDialog,functionFlag::call_cancel);
    }
    else if(myDia->dialogName==MyDialog::MenuDialog)
    {
        myDia->siriWork(MyDialog::MenuDialog,functionFlag::call_cancel);
    }
    else if(myDia->dialogName==MyDialog::setDialog)
    {
        myDia->siriWork(MyDialog::setDialog,functionFlag::call_cancel);
    }
}

//void FromMyDialog::on_pushButton_clicked()
//{
//    this->siri->siriTask(functionFlag::call_cancel);
//}
